package org.simu.loadflow.general;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;

import org.jgrapht.EdgeFactory;

public class BPALFdataImporter implements LFDataImporter {

	private String bpaFileName;	// Name of loadflow data file (BPA format)
	
	public BPALFdataImporter(String bpaFileName) {
		super();
		this.bpaFileName = bpaFileName;
	}

	@Override
	public ACNetwork importData() throws FileNotFoundException {
		// 1. Import loadflow data
		System.out.println("Importing loadflow data in BPA format from " + this.bpaFileName);
		Collection<String> lfDataSet = new ArrayList<String>();
		lfDataSet = readFromFile();
		// 2. Construct and return AC network
		// 2.1. Initialize the network object
		ACNetwork acNetwork = new ACNetwork(ACBranch.class);
		// 2.2. Add AC buses into the network
		for (String thisData : lfDataSet) {
			if (thisData.startsWith("B"))	// Stopped here, create ACBus type objects and added into the network object
				System.out.println(thisData);
		}
		// 2.3. Add AC branches into the network
		return acNetwork;
	}

	// Reading the string collection from the defined BPA loadflow data file
	private Collection<String> readFromFile() throws FileNotFoundException {
		Collection<String> lfDataSet = new ArrayList<String>();
		FileInputStream fs = new FileInputStream(this.bpaFileName);
		InputStreamReader isr = new InputStreamReader(fs);
		BufferedReader br = new BufferedReader(isr);
		try {
			String data = "";
			while ((data = br.readLine()) != null)
				lfDataSet.add(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return lfDataSet;
	}

}